package fun.zhufn.oldcomm.view;

import fun.zhufn.oldcomm.PathManagerController;
import fun.zhufn.oldcomm.model.bo.Path;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ModifyPath {
    private static final PathManagerController pathContro = PathManagerController.getInstance();
    private JFrame frame, father;
    private Path ori = null;
    private boolean isNew = false;

    public ModifyPath() {
        $$$setupUI$$$();
        cancel_jb.addActionListener(e -> {
            frame.dispose();
            father.setVisible(true);
        });
        ok_jb.addActionListener(e -> {
            Path u = new Path(
                    code_jtf.getText(),
                    name_jtf.getText(),
                    inland_jrb.isSelected(),
                    up_jrb.isSelected(),
                    dayweek_jcb.getSelectedIndex(),
                    runtime_jtf.getText(),
                    note_jta.getText()
            );

            if (u.getName().equals("") || u.getCode().equals("")) {
                JOptionPane.showMessageDialog(null, "请不要留空");
                return;
            }
            {
                var t = u.getTime().split(":");
                if (t.length == 2) {
                    try {
                        var h = Integer.parseInt(t[0]);
                        var m = Integer.parseInt(t[1]);
                        if (!(h >= 5 && h < 22 && m >= 0 && m < 60)) {
                            JOptionPane.showMessageDialog(null, "时间格式为24小时制的时:分，且必须在5点以后22点之前");
                            return;
                        }
                    } catch (Exception err) {
                        JOptionPane.showMessageDialog(null, "时间格式为24小时制的时:分，且必须在5点以后22点之前");
                        return;
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "时间格式为24小时制的时:分，且必须在5点以后22点之前");
                    return;
                }
            }

            if (!isNew) pathContro.editPath(u);
            else pathContro.addPath(u);

            frame.dispose();
            father.setVisible(true);
        });
    }

    /**
     * @param path 启动界面
     * @param fa 父界面
     */
    public void start(Path path, JFrame fa) {
        fa.setVisible(false);
        this.isNew = path == null;
        ori = path;
        father = fa;
        frame = new JFrame("ModifyPath");
        var ins = this;
        frame.setContentPane(ins.mu_jp);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setMinimumSize(frame.getSize());
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                father.setVisible(true);
            }
        });
        if (path != null) {
            code_jtf.setText(path.getCode());
            code_jtf.setEnabled(false);
            name_jtf.setText(path.getName());
            inland_jrb.setSelected(path.isInland());
            outland_jrb.setSelected(!path.isInland());
            up_jrb.setSelected(path.isUp());
            down_jrb.setSelected(!path.isUp());
            dayweek_jcb.setSelectedIndex(path.getDay());
            runtime_jtf.setText(path.getTime());
            note_jta.setText(path.getNote());
        }
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (int) (toolkit.getScreenSize().getWidth() - frame.getWidth()) / 2;
        int y = (int) (toolkit.getScreenSize().getHeight() - frame.getHeight()) / 2;
        frame.setLocation(x, y);
    }

    private JPanel mu_jp;
    private JButton ok_jb;
    private JButton cancel_jb;
    private JTextField code_jtf;
    private JLabel code_jlb;
    private JTextField name_jtf;
    private JLabel name_jlb;
    private JLabel dir_jlb;
    private JRadioButton up_jrb;
    private JRadioButton down_jrb;
    private JLabel dayweek_jlb;
    private JComboBox<String> dayweek_jcb;
    private JLabel runtime_jlb;
    private JTextField runtime_jtf;
    private JLabel note_jlb;
    private JTextArea note_jta;
    private JLabel type_jlb;
    private JRadioButton inland_jrb;
    private JRadioButton outland_jrb;


    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        mu_jp = new JPanel();
        mu_jp.setLayout(new GridBagLayout());
        mu_jp.setBackground(new Color(-1116681));
        ok_jb = new JButton();
        ok_jb.setBackground(new Color(-5310023));
        ok_jb.setText("确认");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.weightx = 2.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(ok_jb, gbc);
        cancel_jb = new JButton();
        cancel_jb.setText("取消");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 7;
        gbc.weightx = 2.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(cancel_jb, gbc);
        code_jtf = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mu_jp.add(code_jtf, gbc);
        code_jlb = new JLabel();
        code_jlb.setText("线路代码");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(code_jlb, gbc);
        name_jlb = new JLabel();
        name_jlb.setText("线路名称");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(name_jlb, gbc);
        name_jtf = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(name_jtf, gbc);
        dir_jlb = new JLabel();
        dir_jlb.setText("线路方向");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(dir_jlb, gbc);
        up_jrb = new JRadioButton();
        up_jrb.setSelected(true);
        up_jrb.setText("上行");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(up_jrb, gbc);
        down_jrb = new JRadioButton();
        down_jrb.setText("下行");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(down_jrb, gbc);
        dayweek_jlb = new JLabel();
        dayweek_jlb.setText("运营日期");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(dayweek_jlb, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(dayweek_jcb, gbc);
        runtime_jlb = new JLabel();
        runtime_jlb.setText("发车时间");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(runtime_jlb, gbc);
        runtime_jtf = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(runtime_jtf, gbc);
        note_jlb = new JLabel();
        note_jlb.setText("备注");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(note_jlb, gbc);
        note_jta = new JTextArea();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(note_jta, gbc);
        type_jlb = new JLabel();
        type_jlb.setText("线路类型");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(type_jlb, gbc);
        inland_jrb = new JRadioButton();
        inland_jrb.setSelected(true);
        inland_jrb.setText("岛内");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(inland_jrb, gbc);
        outland_jrb = new JRadioButton();
        outland_jrb.setText("岛外");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(outland_jrb, gbc);
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(down_jrb);
        buttonGroup.add(up_jrb);
        buttonGroup = new ButtonGroup();
        buttonGroup.add(inland_jrb);
        buttonGroup.add(outland_jrb);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mu_jp;
    }

    private void createUIComponents() {
        dayweek_jcb = new JComboBox<>(new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"});
    }
}